package dataGateway;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.derby.jdbc.ClientConnectionPoolDataSource;

import biz.source_code.miniConnectionPoolManager.MiniConnectionPoolManager;


/**
 * @author  jungmins
 */
public class ConnectionPool {
	/**
	 * @uml.property  name="instance"
	 * @uml.associationEnd  
	 */
	private static ConnectionPool instance = null;
	
	/**
	 * @return
	 * @throws SQLException
	 * @uml.property  name="instance"
	 */
	public static ConnectionPool getInstance() throws SQLException
	{
		if(instance == null)
			instance = new ConnectionPool();
		
		return instance;
	}
	
	//*
	// Connection Pool version
	private MiniConnectionPoolManager poolMgr = null;
	private final int DB_CONNECTION_POOL_MAX_CONNECTIONS = 30;
	
	public ConnectionPool() throws SQLException 
	{
		ClientConnectionPoolDataSource cpds = new ClientConnectionPoolDataSource();
		
		// Set the number of statements the cache is allowed to cache.
		// Any number greater than zero will enable the cache.
		cpds.setMaxStatements(20);

		// Set other DataSource properties
		cpds.setServerName("localhost");
		cpds.setPortNumber(1527);
		cpds.setDatabaseName("auction");
		
		poolMgr = new MiniConnectionPoolManager(cpds, DB_CONNECTION_POOL_MAX_CONNECTIONS);
	}

	public Connection getConnection() throws ClassNotFoundException, SQLException
	{
		return poolMgr.getConnection();
	}
//*/

/*
	// DataSource version
	private org.apache.derby.jdbc.ClientDataSource ds =
			   new org.apache.derby.jdbc.ClientDataSource();
	
	public DBUtil() throws SQLException {
		ds.setDatabaseName("auction");
		ds.setServerName("localhost");
		ds.setPortNumber(1527);
	}

	public Connection getConnection() throws ClassNotFoundException, SQLException
	{
		java.sql.Connection con = ds.getConnection();
		return con;
	}
//*/

/*	
	// DriverManager version
	public Connection getConnection() throws ClassNotFoundException, SQLException
	{
		Connection conn = null;
	
		String driver = "org.apache.derby.jdbc.ClientDriver";
		String Url = "jdbc:derby://localhost:1527/auction";
		
		Class.forName(driver); 
		conn = java.sql.DriverManager.getConnection(Url);
		System.out.println("getConnection");

		return conn;
	} //getConnection()
//*/
		
} //class
